/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.entity;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.attr.CefDataAttribute;
import com.inspur.edp.cef.api.attr.ChildAttribute;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.GetDataConvertorGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.NodeDataChangeGetChildGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.NodeDataChangeSerGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.NodeDataSerGetTypesGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.EntityDataSerGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.base.CefNodeDataSerGenerator;
import com.inspur.edp.cef.generator.repository.common.IntermediateType;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.Modifier;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public abstract class EntityNodeDataSerGenerator extends CefNodeDataSerGenerator {
    protected String nodeCode;
    protected boolean isRoot;
    protected IGspCommonObject object;

    public EntityNodeDataSerGenerator(IGspCommonObject object, EntityNodeDataSerGenContext parentSerGenContext, EntityDataSerGenContext edSerGenContext) {
        super(parentSerGenContext, edSerGenContext);
        this.object = object;
        this.nodeCode = object.getCode();
        this.isRoot = object.getParentObject() == null;
    }

    protected EntityNodeDataSerGenContext getParentSerGenContext() {
        return (EntityNodeDataSerGenContext) super.parentSerGenContext;
    }

    protected EntityDataSerGenContext getDataSerGenContext() {
        return (EntityDataSerGenContext) super.dataSerGenContext;
    }

    @Override
    protected void beforeInitialize() {
        super.beforeInitialize();
		MediateType type = new MediateType("" ,getName());
        getDataSerGenContext().getChildTypes().put(nodeCode, type);
        ((EntityDataSerGenContext) dataSerGenContext.getMgrContext().getDataSerContext()).getChildTypes().put(nodeCode, type);
        if (isRoot) {
            dataSerGenContext.setMainObjectType(type);
        } else {
            getParentSerGenContext().getChildSerTypes().put(nodeCode, type);
        }
    }

    protected EntityNodeDataSerGenContext getEntityNodeDataSerGenContext() {
        return (EntityNodeDataSerGenContext) super.getContext();
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        return new EntityNodeDataSerGenContext(nodeCode, isRoot, getDataSerGenContext());
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        return list;
    }

    @Override
    protected String getName() {
        return String.format("%1$sEntitySerConvertor", nodeCode);
    }

    @Override
    protected java.util.ArrayList<BaseGenerator> createClassExtendChildGenrators() {
        java.util.ArrayList<BaseGenerator> list = new java.util.ArrayList<BaseGenerator>();
        for (IGspCommonObject item : object.getContainChildObjects()) {
            list.add(createChildNodeDataSerGenerator(item, getEntityNodeDataSerGenContext(), getDataSerGenContext()));
        }
        return list;
    }

    protected abstract EntityNodeDataSerGenerator createChildNodeDataSerGenerator(IGspCommonObject object, EntityNodeDataSerGenContext context, EntityDataSerGenContext edSerGenContext);

    @Override
    protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators() {
        return new ArrayList<>(Arrays.asList(new ClassMethodGenerator[]
                {
                        new NodeDataSerGetTypesGenerator(object, getEntityNodeDataSerGenContext().getEntityDataSerGenContext().getMgrContext())
                }));
    }
}
